<?php

/*
 * This file is part of jwt-auth.
 *
 * (c) Sean Tymon <tymon148@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Trip\Auth\JWT\Claims;

use App\Trip\Auth\JWT\Support\Utils;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class Factory
{
    /**
     * The request.
     *
     * @var Request
     */
    protected Request $request;

    /**
     * The TTL.
     *
     * @var int
     */
    protected int $ttl = 60;

    /**
     * Time leeway in seconds.
     *
     * @var int
     */
    protected int $leeway = 0;

    /**
     * The classes map.
     *
     * @var array
     */
    private array $classMap
        = [
            'aud' => Audience::class,
            'exp' => Expiration::class,
            'iat' => IssuedAt::class,
            'iss' => Issuer::class,
            'jti' => JwtId::class,
            'nbf' => NotBefore::class,
            'sub' => Subject::class,
        ];

    /**
     * Constructor.
     *
     * @param Request $request
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Generate the initial value and return the Claim instance.
     *
     * @param string $name
     *
     * @return Claim
     */
    public function make(string $name)
    {
        return $this->get($name, $this->$name());
    }

    /**
     * Get the instance of the claim when passing the name and value.
     *
     * @param string $name
     * @param mixed  $value
     *
     * @return Claim
     */
    public function get(string $name, mixed $value)
    {
        if ($this->has($name)) {
            $claim = new $this->classMap[$name]($value);

            return method_exists($claim, 'setLeeway') ?
                $claim->setLeeway($this->leeway) :
                $claim;
        }

        return new Custom($name, $value);
    }

    /**
     * Check whether the claim exists.
     *
     * @param string $name
     *
     * @return bool
     */
    public function has(string $name): bool
    {
        return array_key_exists($name, $this->classMap);
    }

    /**
     * Set the leeway in seconds.
     *
     * @param int $leeway
     *
     * @return $this
     */
    public function setLeeway(int $leeway): static
    {
        $this->leeway = $leeway;

        return $this;
    }

    /**
     * Get the Issuer (iss) claim.
     *
     * @return string
     */
    public function iss(): string
    {
        return $this->request->url();
    }

    /**
     * Get the Issued At (iat) claim.
     *
     * @return int
     */
    public function iat(): int
    {
        return Utils::now()->getTimestamp();
    }

    /**
     * Get the Expiration (exp) claim.
     *
     * @return int
     */
    public function exp(): int
    {
        return Utils::now()->addMinutes($this->ttl)->getTimestamp();
    }

    /**
     * Get the Not Before (nbf) claim.
     *
     * @return int
     */
    public function nbf(): int
    {
        return Utils::now()->getTimestamp();
    }

    /**
     * Get the JWT Id (jti) claim.
     *
     * @return string
     */
    public function jti(): string
    {
        return Str::random();
    }

    /**
     * Add a new claim mapping.
     *
     * @param string $name
     * @param string $classPath
     *
     * @return $this
     */
    public function extend(string $name, string $classPath): static
    {
        $this->classMap[$name] = $classPath;

        return $this;
    }

    /**
     * Set the request instance.
     *
     * @param Request $request
     *
     * @return $this
     */
    public function setRequest(Request $request): static
    {
        $this->request = $request;

        return $this;
    }

    /**
     * Get the token ttl.
     *
     * @return int
     */
    public function getTTL(): int
    {
        return $this->ttl;
    }

    /**
     * Set the token ttl (in minutes).
     *
     * @param int $ttl
     *
     * @return $this
     */
    public function setTTL(int $ttl): static
    {
        $this->ttl = $ttl;

        return $this;
    }
}
